//
//  HJYSearchHistoryView.m
//  HuiJiaYouPad
//
//  Created by hjc on 15/4/17.
//  Copyright (c) 2015年 qhsy. All rights reserved.
//
//  搜索历史记录

#define HJYMaxLabelCount 10
#define HJYMaxLocPerRow  3
#define HJYLabelW        ((_searchRecordView.frame.size.width - HJYLabelSpace) * 0.3333333)
#define HJYLabelH        30
#define HJYLabelSpace    8


#import "HJYSearchHistoryView.h"
#import "searchhistory.h"

@interface HJYSearchHistoryView () <UIAlertViewDelegate>
{
    UIView   *_searchRecordView;
}
@end

@implementation HJYSearchHistoryView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor purpleColor];
        
        // 添加头部
        UIView *titleView = [[UIView alloc] init];
//        titleView.backgroundColor = [UIColor yellowColor];
        CGFloat titleViewX = 10 * 2;
        CGFloat titleViewY = 10 * 2;
        titleView.frame = CGRectMake(titleViewX, titleViewY, self.frame.size.width - titleViewX * 2, 20);
        [self addSubview:titleView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"最近搜索";
        titleLabel.textColor = [UIColor redColor];
        titleLabel.font = [UIFont systemFontOfSize:13];
        CGFloat titleLabelW = 100;
        CGFloat titleLabelH = titleView.frame.size.height;
        CGFloat titleLabelX = 0;
        CGFloat titleLabelY = titleView.frame.size.height - titleLabelH;
        titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
        [titleView addSubview:titleLabel];
        
        UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [clearBtn setTitle:@"清空" forState:UIControlStateNormal];
        [clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        clearBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        CGFloat clearBtnW = 40;
        CGFloat clearBtnH = titleView.frame.size.height;
        CGFloat clearBtnX = titleView.frame.size.width - clearBtnW;
        CGFloat clearBtnY = titleView.frame.size.height - clearBtnH;
        clearBtn.frame = CGRectMake(clearBtnX, clearBtnY, clearBtnW, clearBtnH);
        [clearBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:clearBtn];
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor blackColor];
        CGFloat lineW = titleView.frame.size.width;
        CGFloat lineH = 0.5;
        CGFloat lineY = titleView.frame.size.height - lineH;
        line.frame = CGRectMake(0, lineY, lineW, lineH);
        [titleView addSubview:line];
        
        
        
        // 最近的搜索记录
        _searchRecordView = [[UIView alloc] init];
//        _searchRecordView.backgroundColor = [UIColor yellowColor];
        _searchRecordView.frame = CGRectMake(10 * 2, CGRectGetMaxY(titleView.frame), titleView.frame.size.width, self.frame.size.height - CGRectGetMaxY(titleView.frame));
        [self addSubview:_searchRecordView];
        
        for (NSUInteger i = 0; i < HJYMaxLabelCount; i++) {
            UILabel *strLabel = [[UILabel alloc] init];
            strLabel.textColor=[UIColor blueColor];
            strLabel.textAlignment= NSTextAlignmentCenter;
//            strLabel.backgroundColor = [UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0];
            strLabel.userInteractionEnabled = YES;
            [strLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchLabelClick:)]];
            NSUInteger loc = i %  HJYMaxLocPerRow;
            NSUInteger row = i /  HJYMaxLocPerRow;
            CGFloat strLabelX = loc * (HJYLabelW + HJYLabelSpace);
            CGFloat strLabelY = row * (HJYLabelH + HJYLabelSpace);
            strLabel.frame = CGRectMake(strLabelX, strLabelY, HJYLabelW, HJYLabelH);
//            strLabel.backgroundColor = [UIColor redColor];
            [_searchRecordView addSubview:strLabel];
        }
        
        // 加载初始数据
        NSUInteger count = [[searchhistory getSearchString] count];
        for (NSUInteger i = 0; i < HJYMaxLabelCount; i++) {
            if (i < count) {
                UILabel *label = _searchRecordView.subviews[i];
                label.hidden = NO;
                label.text = [searchhistory getSearchString][i];
                label.textColor=[UIColor blueColor];
            } else {
                UILabel *label = _searchRecordView.subviews[i];
                label.hidden = YES;
            }
        }
    }
    return self;
}

- (void)searchLabelClick:(UITapGestureRecognizer *)recognizer
{
    UILabel *label = (UILabel *)recognizer.view;
    if ([_delegate respondsToSelector:@selector(searchHistoryView:searchText:)]) {
        [_delegate searchHistoryView:self searchText:label.text];
    }
    [self setSearchString:label.text];
}

- (void)setSearchString:(NSString *)searchString
{
    _searchString = searchString;
    
    // 更新数据的存储和显示
    [searchhistory saveSearchString:_searchString];
    NSUInteger count = [[searchhistory getSearchString] count];
    for (NSUInteger i = 0; i < HJYMaxLabelCount; i++) {
        if (i < count) {
            UILabel *label = _searchRecordView.subviews[i];
            label.hidden = NO;
            label.text = [searchhistory getSearchString][i];
        } else {
            UILabel *label = _searchRecordView.subviews[i];
            label.hidden = YES;
        }
    }
}



- (void)clearBtnClick
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清除历史记录" message:@"即将清除历史记录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [searchhistory removeAllSearchRecord];
        
        // 更新显示
        for (NSUInteger i = 0; i < HJYMaxLabelCount; i++) {
            UILabel *label = _searchRecordView.subviews[i];
            label.hidden = YES;
        }
    }
}












@end
